package com.photoeditor.demo.util;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.lang.reflect.Type;
import java.util.List;

/**
 * json处理类
 */
public class JsonUtil {

	private static final Gson gson = new Gson();
	private static final JsonParser parser = new JsonParser();
	private static final Gson prettyGson = new GsonBuilder().setPrettyPrinting() // 对json结果格式化.
			.create();

	public static String toJsonString(Object obj) {
		return gson.toJson(obj);
	}

	public static String toPrettyJsonString(Object obj) {
		return prettyGson.toJson(obj);
	}

	public static <T> T fromJsonString(String jsonString, Class<T> classofT) {
		return gson.fromJson(jsonString, classofT);
	}

	public static <T> T fromJsonString(String jsonString, Type type) {
		return gson.fromJson(jsonString, type);
	}

	public static <T> T fromJsonObject(JsonObject jsonObj, Class<T> classofT) {
		return gson.fromJson(gson.toJson(jsonObj), classofT);
	}

	public static <T> T fromJsonObject(String jsonObj, Type type) {
		return gson.fromJson(gson.toJson(jsonObj), type);
	}

	public static JsonObject toJsonObject(String jsonString) {
		return parser.parse(jsonString).getAsJsonObject();
	}

	public static JsonObject toJsonObject(Object obj) {
		return parser.parse(toJsonString(obj)).getAsJsonObject();
	}

	public static String toJsonString(List<String> strings){
		return gson.toJson(strings);
	}
	
}